#ifndef SENSORS_H
#define SENSORS_H


#include <QtGui>
#include <windows.h>

#include "chrum6lt.h"
#include "cyberglove.h"
#include "quaternionindicator.h"
#include "simsock.h"

const int COUNT_OF_SENSORS = 4;

struct ArmProfile
{
    QString name;
    float imu[5][7];
    short glove[3][8];
};

class Sensors : public QWidget
{
    Q_OBJECT

public:
    explicit Sensors(const QVector<QString> &ports,bool isLeft = false, bool is3IMUFormat = false, bool isVisible = false, QWidget *parent = 0);

    void changeIMUFormat(bool is3IMUFormat);
    void changeArmSide(bool isLeft) {isLeftArm = isLeft;}

signals:
    void deviceConnected(char ind, QString port);
    void gloveCommand(short channel, int command);
//    void pneumoCommand(char ind, int value);
    void sendStringCommand(QString str, int com);
    void sendRecordCommand(QString str, int com);
    void standBySignal(QString str);
    void sendQuaternion(int ind, QQuaternion quat);
    void sendAngles(int ind, float ang1, float ang2);

private:
    typedef QMap<QString, QString> StringMap;

    void setWindow();
    void saveProfiles();
    void loadProfiles();
    void setProfileSettings();
    void setDefaultProfile();

    void shoulderProcessing();
    void shoulderCommands();
    void forearmProcessing();
    void forearmCommands();
    void handProcessing();
    void handCommands();
    void checkBoundaries(int ind, float &val);
    void sendCommand(int ind, int com);

    ChrUM6LT    *shoulderIMU;
    ChrUM6LT    *forearmIMU;
    ChrUM6LT    *handIMU;
    Cyberglove  *cyberglove;
    QMap<char, QUart*> uartSensors;
    QVector<QString> defaultPorts;

    QVector<QuaternionIndicator*> indicator;

    QCheckBox *autoConnect;
    QPushButton *connectAllBtn;

    QComboBox *profilesBox;
    QPushButton *newBtn;
    QPushButton *deleteBtn;
    QPushButton *calibrateBtn;
    QPushButton *zeroGyroBtn;
    QPushButton *manualBtn;
    QPushButton *sendBtn;
    QPushButton *sendArmBtn;
    QPushButton *sendHandBtn;
    QPushButton *fingerTonusBtn;
    QCheckBox *useBoundariesBox;
    QCheckBox *recordCommandBox;
    QGridLayout *handGrid;
    QPushButton *gloveCalibrateBtn;

    QMap<short, QLineEdit*> settingsMap;
    QMap<short, QSlider*>   sliderMap;
    QVector<QWidget*> twoIMUWidgets;
    QVector<QWidget*> threeIMUWidgets;
    bool threeIMUFormat;
    bool isLeftArm;
    int soundValue;
    char gloveCount;
    bool fingerTonusFlag;

    bool armSendFlag;
    bool handSendFlag;

    QVector<ArmProfile> profilesVector;
    ArmProfile *currentProfile;

    QQuaternion currentQuaternions[3];
    QQuaternion calibrateQuaternions[3];
    float currentAngles[7];
    unsigned short currentGloveData[8];

    bool recordStatus;
    bool clientIsConnected;

private slots:
    void onConnected(QString port);
    void onDevicesChanged();
    void onConnectAllBtn();

    void onManualBtn(bool bl);
    void onUseBoundariesBox(bool bl);
    void onRecordCommandBox(bool bl) {recordStatus = bl;}
    void onProfBoxChanged(int ind);
    void profNameChanged(QString name);
    void onNewBtn();
    void onDeleteBtn();
    void onCalibrateBtn();
    void onZeroGyroBtn();
    void onSendBtn(bool bl);

    void settingsChanged();
    void sliderChanged(int val);

    void takeGloveData(uchar channel, short data);
    void takeQuaternion(QQuaternion quat);
    void onGlovaCalibrate();
    void onFingerTonusBtn(bool bl);
    void onRecordCliendRecived(QString, SimpleSocket::StringMap);

};


#endif // SENSORS_H
